<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="商品名称" prop="specName">
        <el-input
          v-model="queryParams.specName"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
        <el-form-item label="供应状态" prop="supplyStatus">
        <el-select
          v-model="queryParams.supplyStatus"
          clearable
          @change="handleQuery"
        >
          <el-option :value="null" label="全部"/>
          <el-option :value="1" label="供应"/>
          <el-option :value="0" label="未供应"/>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-refresh"
          size="mini"
          @click="handleSync"
        >
          同步
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="goodList" @selection-change="handleSelectionChange">
      <el-table-column label="商品Id" align="center" prop="goodsId" />
      <el-table-column label="商品Sku" align="center" prop="goodsSku" />
      <el-table-column label="品牌名称" align="center" prop="goodsName" />
      <el-table-column label="业务类型" align="center" prop="bName" />
      <el-table-column label="商品分类" align="center" prop="venderName" />
      <el-table-column label="商品名称" align="center" prop="specName" />
      <el-table-column label="商品编码" align="center" prop="externalCode" />
      <el-table-column label="供货价" align="center" prop="userQuotePayment" />
      <el-table-column label="供货接口" align="center" prop="supplierService">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.card_source" :value="scope.row.supplierService"/>
        </template>
      </el-table-column>
      <el-table-column label="供应状态" align="center" prop="supplyStatus" >
        <template v-slot="{ row }">
          <el-tag v-if="row.supplyStatus">供应</el-tag>
          <el-tag v-else type="danger">未供应</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="官方商品名称" align="center" prop="officialProductName" />
      <el-table-column label="同步时间" align="center" prop="updateTime" width="160"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >
            修改
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改mf178对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="官方商品名称" prop="officialProductName">
          <el-input v-model="form.officialProductName" placeholder="请输入商品名称" />
        </el-form-item>
        <el-form-item label="供货接口" prop="supplierService">
          <el-select v-model="form.supplierService" clearable>
            <el-option
              v-for="dict in dict.type.card_source"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listGood, getGood, delGood, addGood, updateGood , syncGood} from "@/api/mf178/good"

export default {
  name: "Good",
  dicts: ['card_source'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // mf178表格数据
      goodList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        specName: null,
        supplyStatus: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询mf178列表 */
    getList() {
      this.loading = true
      listGood(this.queryParams).then(response => {
        this.goodList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        goodsId: null,
        goodsSku: null,
        goodsName: null,
        bName: null,
        venderName: null,
        specName: null,
        specValueIds: null,
        externalCode: null,
        userQuotePayment: null,
        supplierService: null,
        supplyStatus: null,
        officialProductName: null,
        officialPrice: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      }
      this.resetForm("form")
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.goodsId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = "添加"
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const goodsId = row.goodsId || this.ids
      getGood(goodsId).then(response => {
        this.form = response.data
        this.open = true
        this.title = "修改"
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.goodsId != null) {
            updateGood(this.form).then(response => {
              this.$modal.msgSuccess("修改成功")
              this.open = false
              this.getList()
            })
          } else {
            addGood(this.form).then(response => {
              this.$modal.msgSuccess("新增成功")
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    async handleSync() {
      await this.$modal.confirm('确定同步？');
      const loading = this.$loading();
      try {
        await syncGood();
        this.$message.success('同步成功');
        this.getList();
      }finally {
        loading.close();
      }
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const goodsIds = row.goodsId || this.ids
      this.$modal.confirm('是否确认删除mf178编号为"' + goodsIds + '"的数据项？').then(function() {
        return delGood(goodsIds)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('mf178/good/export', {
        ...this.queryParams
      }, `good_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>
